<template>
  <div class="tab-bar">
    <template v-for="(tabbarItem, index) in tabbarData">
      <div
        class="tab-bar-item"
        :class="{ active: currentIndex === index }"
        @click="itemClick(index, tabbarItem)"
      >
        <img
          v-if="currentIndex !== index"
          :src="getAssetUrl(tabbarItem.image)"
        />
        <img v-else :src="getAssetUrl(tabbarItem.imageActive)" />
        <span>{{ tabbarItem.text }}</span>
      </div>
    </template>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import tabbarData from "@/assets/data/tabbar.js";
import getAssetUrl from "@/utils/get_assets_image.js";

const router = useRouter();
// console.log(router);
const currentIndex = ref(0);
const itemClick = (index, tabbarItem) => {
  // 1.将index的值传递给currentIndex
  currentIndex.value = index;
  // 2.编程时导航
  router.push(tabbarItem.path);
};
</script>

<style lang="less" scoped>
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  display: flex;
  border-top: 1px solid #f3f3f3;
  .tab-bar-item {
    flex: 1;
    flex-direction: column;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    &.active {
      color: var(--primary-color);
    }
    img {
      width: 36px;
    }

    span {
      margin-top: 4px;
      font-size: 12px;
    }
  }
}
</style>
